home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / gnome_sudoku / printing.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  4.2 KB  |  114 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import gtk
  5. import cairo
  6. import sudoku
  7. import gsudoku
  8. from gettext import gettext as _
  9. from gettext import ngettext
  10.  
  11. def fit_squares_in_rectangle(width, height, n, margin = 0):
  12.     '''Optimally fit squares into a rectangle.
  13.  
  14.     Return number of columns, number of rows, and square size
  15.     for the best fit of n squares into a rectangle of given width and height.
  16.  
  17.     Optionally include a margin along edges and between squares.
  18.     '''
  19.     best_square_size = 0
  20.     best_fit = None
  21.     for n_across in range(1, int(n) + 1):
  22.         if not n % n_across or 1:
  23.             pass
  24.         n_down = n / n_across + 0
  25.         across_size = width - (n_across + 1) * margin
  26.         across_size = across_size / n_across
  27.         down_size = height - (n_down + 1) * margin
  28.         down_size = down_size / n_down
  29.         if across_size < down_size:
  30.             square_size = across_size
  31.         else:
  32.             square_size = down_size
  33.         if square_size > best_square_size:
  34.             best_square_size = square_size
  35.             best_fit = (n_across, n_down)
  36.             continue
  37.     
  38.     if best_fit:
  39.         return (best_fit, best_square_size)
  40.  
  41.  
  42. class SudokuPrinter:
  43.     
  44.     def __init__(self, sudokus, main_window, margin = 50, sudokus_per_page = 1):
  45.         self.drawn = False
  46.         self.margin = margin
  47.         self.sudokus_per_page = sudokus_per_page
  48.         self.n_sudokus = len(sudokus)
  49.         self.sudokus = sudokus
  50.         self.print_op = gtk.PrintOperation()
  51.         self.print_op.connect('begin-print', self.begin_print)
  52.         self.print_op.connect('draw-page', self.draw_page)
  53.         self.main_window = main_window
  54.  
  55.     
  56.     def begin_print(self, operation, context):
  57.         import math
  58.         pages = int(math.ceil(self.n_sudokus / self.sudokus_per_page))
  59.         operation.set_n_pages(pages)
  60.  
  61.     
  62.     def draw_page(self, operation, context, page_nr):
  63.         import pango
  64.         import sudoku_thumber
  65.         margin = 25
  66.         cr = context.get_cairo_context()
  67.         width = context.get_width()
  68.         height = context.get_height()
  69.         (best_fit, best_square_size) = fit_squares_in_rectangle(width, height, self.sudokus_per_page, margin)
  70.         start = int(page_nr * self.sudokus_per_page)
  71.         sudokus_on_page = self.sudokus[start:start + int(self.sudokus_per_page)]
  72.         left = margin
  73.         top = margin
  74.         pos = [
  75.             1,
  76.             1]
  77.         for sudoku in sudokus_on_page:
  78.             if type(sudoku) == tuple:
  79.                 label = sudoku[1]
  80.                 sudoku = sudoku[0]
  81.             else:
  82.                 label = ''
  83.             cr.set_font_size(12)
  84.             cr.select_font_face('', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
  85.             cr.set_source_rgb(0, 0, 0)
  86.             (xbearing, ybearing, width, height, xadvance, yadvance) = cr.text_extents(label)
  87.             cr.move_to(left, top - height / 2)
  88.             cr.show_text(label)
  89.             if isinstance(sudoku, gsudoku.SudokuGameDisplay):
  90.                 sudoku = sudoku.grid
  91.             
  92.             sudoku_thumber.draw_sudoku(cr, sudoku.grid, None, best_square_size, left, top)
  93.             if pos[0] < best_fit[0]:
  94.                 left = left + best_square_size + margin
  95.                 pos[0] += 1
  96.                 continue
  97.             top = top + best_square_size + margin
  98.             left = margin
  99.             pos[0] = 1
  100.             pos[1] += 1
  101.         
  102.  
  103.  
  104.  
  105. def print_sudokus(*args, **kwargs):
  106.     sp = SudokuPrinter(*args, **kwargs)
  107.     res = sp.print_op.run(gtk.PRINT_OPERATION_ACTION_PRINT_DIALOG, sp.main_window)
  108.     if res == gtk.PRINT_OPERATION_RESULT_ERROR:
  109.         error_dialog = gtk.MessageDialog(main_window, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, 'Error printing file:\n')
  110.         error_dialog.connect('response', (lambda w, id: w.destroy()))
  111.         error_dialog.show()
  112.     
  113.  
  114.